package com.example.demo.study.book;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BookManagement {
    private List<Book> books = new ArrayList<>();
    private Scanner scanner = new Scanner(System.in);
    private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    // 添加图书
    public void addBook(){
        System.out.print("请输入ISBN: ");
        String isbn = scanner.next();
        System.out.print("请输入书名: ");
        String title = scanner.next();
        System.out.print("请输入作者: ");
        String author = scanner.next();
        LocalDate publicationDate = getValidDateInput("请输入出版日期 (格式: yyyy-MM-dd): ");
        books.add(new Book(isbn,title,author,publicationDate));
        System.out.println("添加图书成功");
    }
    // 删除图书
    public void delBook(){
        System.out.print("请输入要删除的图书ISBN: ");
        String isbn = scanner.next();
        boolean removed = books.removeIf(book -> book.getIsbn().equals(isbn));
        if (removed) {
            System.out.println("图书删除成功！");
        } else {
            System.out.println("未找到该ISBN的图书！");
        }
    }
    // 修改图书
    public void updateBook(){
        System.out.print("请输入要修改的图书ISBN: ");
        String isbn = scanner.nextLine();

        Book bookToUpdate = findBookByIsbn(isbn);
        if (bookToUpdate == null) {
            System.out.println("未找到该ISBN的图书！");
            return;
        }

        System.out.print("请输入新书名 (不修改请直接回车): ");
        String newTitle = scanner.nextLine();
        if (!newTitle.isEmpty()) {
            bookToUpdate.setTitle(newTitle);
        }

        System.out.print("请输入新作者 (不修改请直接回车): ");
        String newAuthor = scanner.nextLine();
        if (!newAuthor.isEmpty()) {
            bookToUpdate.setAuthor(newAuthor);
        }

        System.out.print("请输入新出版日期 (不修改请直接回车，格式: yyyy-MM-dd): ");
        String dateInput = scanner.nextLine();
        if (!dateInput.isEmpty()) {
            try {
                LocalDate newDate = LocalDate.parse(dateInput, dateTimeFormatter);
                bookToUpdate.setPublicationDate(newDate);
            } catch (DateTimeParseException e) {
                System.out.println("日期格式错误，出版日期未修改！");
            }
        }

        System.out.println("图书信息修改成功！");
    }
    // 查询图书
    public void searchBook(){
        if (books.isEmpty()) {
            System.out.println("图书馆暂无图书记录！");
            return;
        }
        System.out.println("\n===== 图书馆所有图书 =====");
        books.forEach(System.out::println);
    }
    // 根据id查询图书
    public void findBookById(){
        System.out.print("请输入要查询的图书ISBN: ");
        String isbn = scanner.nextLine();
        Book book = findBookByIsbn(isbn);
        if (book != null) {
            System.out.println(book);
        } else {
            System.out.println("未找到该ISBN的图书！");
        }
    }

    private  LocalDate getValidDateInput(String prompt){
        while (true){
            System.out.println(prompt);
            String dateStr = scanner.next();
            try {
                return LocalDate.parse(dateStr);
            }catch (Exception e){
                System.out.println(e.getMessage());
            }
        }
    }
    private  Book findBookByIsbn(String isbn){
        for (Book book : books) {
            if (book.getIsbn().equals(isbn)) {
                return book;
            }
        }
        return null;
    }
}
